/*
 *  Copyright 2010 Farent-Solutions Ltd.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.farent.solutions.plugins.aribaweb.ui;

import com.farent.solutions.plugins.aribaweb.AWNetbeansProject;
import com.farent.solutions.plugins.aribaweb.NBawConstants;
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import org.netbeans.api.project.Project;
import org.netbeans.spi.project.ui.CustomizerProvider;
import org.netbeans.spi.project.ui.support.ProjectCustomizer;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author user
 */
public class CustomizerProviderImpl implements CustomizerProvider {

    private AWNetbeansProject project;
    public static final String CUSTOMIZER_FOLDER_PATH = "Projects/"
            + NBawConstants.PROJECT_PATH
            + "/Customizer"; //NO18N

    public CustomizerProviderImpl(AWNetbeansProject project) {
        this.project = project;
    }

    @Override
    public void showCustomizer() {
        showCustomizer(null);
    }

    public void showCustomizer(String preselectedCategory) {
        OptionListener listener = new OptionListener(project);
        Lookup context = Lookups.fixed(new Object[]{project});
        Dialog dialog = ProjectCustomizer.createCustomizerDialog(
                CUSTOMIZER_FOLDER_PATH, context, null, listener, null );
        dialog.addWindowListener(listener);
        listener.setDialog(dialog);
        dialog.setTitle("Aribaweb project properties");
        dialog.setModal(true);
        dialog.setVisible(true);
    }

    /** Listens to the actions on theF Customizer's option buttons */
    private class OptionListener extends WindowAdapter implements ActionListener {

        private Dialog dialog;
        private Project project;

        void setDialog(Dialog dlg) {
            dialog = dlg;
        }

        OptionListener(Project project) {
            this.project = project;
        }

        // Listening to OK button ----------------------------------------------
        public void actionPerformed(ActionEvent e) {

            if (dialog != null) {
                dialog.setVisible(false);
                dialog.dispose();
            }
        }

        // Listening to window events ------------------------------------------
        @Override
        public void windowClosed(WindowEvent e) {
        }

        @Override
        public void windowClosing(WindowEvent e) {
            if (dialog != null) {
                dialog.setVisible(false);
                dialog.dispose();
            }
        }
    }
}
